layui.use(['element', 'layer', 'form', 'table'], function () {
    var element = layui.element;
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;

    var tableInfo = table.render({
        id: 'repairInfoTableId',
        elem: '#repairInfoTable',
        url: '/admin/repairInfo/houseInfoDetail',
        method: 'post',
        page: true,
        limit: 10,
        loading: true,
        skin: 'row',
        even: true,
        text: {
            none: '暂无相关数据'
        },
        cols: [[
            {checkbox: true, fixed: true},
            {field: 'id', title: '序号', type: 'numbers', unresize: true, sort: true, width: 80, fixed: 'left'}
            , {field: 'userId', title: '报修人', unresize: true, width: 80}
            , {field: 'phone', title: '联系电话',align: 'center'}
            , {field: 'address', title: '地址',align: 'center'}
            , {field: 'title', title: '标题',align: 'center'}
            , {field: 'reason', title: '原因', align: 'center'}
            , {field: 'statusStr', title: '状态', unresize: true, align: 'center', width: 80}
            , {field: 'createTime', title: '创建时间', sort: true, unresize: true, align: 'center'}
            , {field: 'updateTime', title: '更新时间', sort: true, unresize: true, align: 'center'}
            , {fixed: 'right', title: '操作', width: 150, align: 'center', unresize: true, toolbar: '#oreationBtn'}
        ]]
    });

    table.on('tool(repairInfoTable)', function (obj) {
        var layEvent = obj.event;
       if (layEvent === 'del') { //删除
            deleteFunction(obj);
        } else if (layEvent === 'update') { //更新
            updateFunction(obj);
        }
    });
    
    function deleteFunction(obj) {
        layer.open({
            type: 1,
            title: false,
            closeBtn: 2,
            resize: true,
            content: '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">' +
            '(请慎重)是否需要删除?删除后将不可恢复</div>',
            area: '30%',
            shade: 0.8,
            shadeClose: true,
            anim: 1,
            id: 'LAY_layuiprodel',
            btn: ['绝不后悔', '我怂了'],
            moveType: 1,
            yes: function (index, layero) {
                var params = $.param({
                    id: obj.data.id
                }, true);
                $.ajax({
                    url: '/admin/repairInfo/deleteById',
                    type: 'post',
                    data: params,
                    dataType: 'json',
                    success: function (e) {
                        layer.msg(e.msg);
                        layer.close(index);
                        if (e.msg == "删除成功") {
                            obj.del();
                        }
                    }
                })
            }
        });
    }
    
    function updateFunction(obj) {
        var content = $('#updateRepairInfo');
        layer.open({
            type: 1,
            skin: 'layui-layer-molv',
            title: ['更新基本数据', 'font-size:18px;'],
            closeBtn: 1,
            shade: 0.3,
            content: content,
            area: ['70%', '70%'],
            shadeClose: true,
            resize: true,
            anim: 1,
            id: 'LAY_layuiroupdate', //设定一个id，防止重复弹出
            moveType: 1,
            maxmin: false,
            success: function (layero, index) {
                $('#userId').val(obj.data.userId);
                $('#phone').val(obj.data.phone);
                $('#address').val(obj.data.address);
                $('#title').val(obj.data.title);
                $('#reason').val(obj.data.reason);
                $('#status').val(obj.data.status);
                form.render();
                var active = {
                    updateInfo:function () {
                        var userId =  $('#userId').val();
                        var phone =  $('#phone').val();
                        var address =  $('#address').val();
                        var title =  $('#title').val();
                        var reason =  $('#reason').val();
                        var status =  $('#status').val();
                        var statusStr = $('#status option:selected').text();
                        if (userId == '' || phone == '' || address =='' || title== '' || reason =='') {
                            layer.msg('数据不可为空');
                            return false;
                        }
                        if(userId == obj.data.userId &&  phone == obj.data.phone
                            && address ==obj.data.address && title ==obj.data.title &&
                            reason ==obj.data.reason && status == obj.data.status) {
                            layer.msg('未做任何修改');
                            return false;
                        }
                        var params = $.param({
                            id:obj.data.id,
                            userId: userId,
                            phone: phone,
                            address: address,
                            title: title,
                            reason: reason,
                            status: status
                        }, true);
                        $.ajax({
                            url: '/admin/repairInfo/updateRepairInfo',
                            type: 'post',
                            data: params,
                            dataType: 'json',
                            success: function (e) {
                                layer.msg(e.msg);
                                layer.close(index);
                                if (e.msg == "更新成功") {
                                    //同步更新缓存对应的值
                                    obj.update({
                                        userId: userId,
                                        phone: phone,
                                        address:address,
                                        title:title,
                                        reason:reason,
                                        statusStr: statusStr,
                                        status:status
                                    });
                                }
                            }
                        })
                    }
                };
                $('.updateProperty .layui-btn').on('click', function () {
                    var type = $(this).data('type');
                    active[type] ? active[type].call(this) : '';
                });
            },
            cancel: function(index, layero){
                $('#userId').val('');
                $('#phone').val('');
                $('#address').val('');
                $('#title').val('');
                $('#reason').val('');
                $('#status').val(0);
                form.render();
            }
        })
    }

    var active = {
        findRepairInfo:function () {
            var content = $('#findRepairInfo');
            layer.open({
                type: 1,
                skin: 'layui-layer-molv',
                title: ['查询维度', 'font-size:18px;'],
                closeBtn: 1,
                shade: 0.3,
                content: content,
                area: ['70%', '70%'],
                shadeClose: false,
                resize: true,
                anim: 1,
                id: 'LAY_layuiroupdate', //设定一个id，防止重复弹出
                moveType: 1,
                maxmin: false,
                success: function (layero, index) {
                    $('#userId').val('');
                    $('#phone').val('');
                    $('#address').val('');
                    $('#title').val('');
                    $('#reason').val('');
                    $('#status').val(0);
                    form.render();
                    var active = {
                        findInfo:function () {
                            var userId =  $('#fuserId').val();
                            var phone =  $('#fphone').val();
                            var address =  $('#faddress').val();
                            var title =  $('#ftitle').val();
                            var reason =  $('#freason').val();
                            var status =  $('#fstatus').val();
                            var params ={
                                userId: userId,
                                phone: phone,
                                address: address,
                                title: title,
                                reason: reason,
                                status: status
                            };
                            tableInfo.reload({
                                where: {
                                    key: JSON.stringify(params) //转成json
                                },
                                page: {
                                    curr: 1 //重新从第 1 页开始
                                },
                                done: function (res, curr, count) {
                                    this.where = {};
                                    layer.close(index);
                                }
                            });
                        }
                    };
                    $('.findProperty .layui-btn').on('click', function () {
                        var type = $(this).data('type');
                        active[type] ? active[type].call(this) : '';
                    });
                },
                cancel: function(index, layero){
                    $('#fuserId').val('');
                    $('#fphone').val('');
                    $('#faddress').val('');
                    $('#ftitle').val('');
                    $('#freason').val('');
                    $('#fstatus').val(0);
                    form.render();
                }
            })
        },
        deletebatchInfo:function () {
            var checkStatus = table.checkStatus('repairInfoTableId')
                , data = checkStatus.data;
            if (data.length > 50) {
                layer.msg("超过最大数量限制");
                return false;
            }
            layer.open({
                type: 1,
                title: false,
                closeBtn: 2,
                resize: true,
                content: '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">' +
                '是否需要删除?删除后将不可恢复</div>',
                area: '30%',
                shade: 0.8,
                shadeClose: true,
                anim: 1,
                id: 'LAY_layuiprodel', //设定一个id，防止重复弹出
                btn: ['绝不后悔', '我怂了'],
                moveType: 1,
                yes: function (index, layero) {
                    var params = $.param({
                        mydata: JSON.stringify(data)
                    }, true);
                    $.ajax({
                        url: '/admin/repairInfo/deleteBatchById',
                        type: 'post',
                        data: params,
                        dataType: 'json',
                        success: function (e) {
                            layer.msg(e.msg);
                            layer.close(index);
                            if (e.msg == "删除成功") {
                                tableInfo.reload({ //执行重载
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    },
                                    done: function (res, curr, count) {
                                        this.where = {};
                                    }
                                });
                            }
                        }
                    })
                }
            });
        },
        addRepairInfo:function () {
            var userId =  $('#myuserId').val();
            var phone =  $('#myphone').val();
            var address =  $('#myaddress').val();
            var title =  $('#mytitle').val();
            var reason =  $('#myreason').val();
            if (userId == '' || phone == '' || address =='' || title== '' || reason =='') {
                layer.msg('数据不可为空');
                return false;
            }
            var params = $.param({
                userId: userId,
                phone: phone,
                address: address,
                title: title,
                reason: reason
            }, true);
            $.ajax({
                url: '/admin/repairInfo/addOneRepair',
                type: 'post',
                data: params,
                dataType: 'json',
                success: function (e) {
                    layer.msg(e.msg);
                    if (e.msg == "添加成功") {
                        tableInfo.reload({ //执行重载
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        });
                        $('#myuserId').val('');
                        $('#myphone').val('');
                        $('#myaddress').val('');
                        $('#mytitle').val('');
                        $('#myreason').val('');
                    }
                }
            })
        }
    };

    //全局操作按钮
    $('.AllBtnController .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    $('.addRepairInfo .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });


});